<?php
/**
 * Created by PhpStorm.
 * User: xiaoxin
 * Date: 2018/11/15 0015
 * Time: 13:53
 */

namespace app\admin\controller;
use app\admin\model\Admin;
use app\admin\model\AuthRole;
use think\Controller;
use think\Loader;
use think\Db;

class Admins extends Controller
{
	/**
	 * 管理员列表页面
	 * @return mixed
	 * @throws \think\exception\DbException
	 */
	public function index(){
		$role = AuthRole::all();
		$where = [];
		input('role_id') && $where['t3.role_id'] = input('role_id');
		input('nickname') && $where['t2.nickname'] = ['like','%'.input('nickname').'%'];
		$admin = Db::name('auth_role_admin')
			->field('t2.*,t3.role_name')
			->alias('t1')
			->join('admin t2','t1.admin_id = t2.admin_id')
			->join('auth_role t3','t1.role_id = t3.role_id')
			->group('t1.admin_id')
			->where($where)
			->where('t2.delete_time',null)
			->paginate(10);
		if (!$admin->items()){
			$this->error('沒有該管理者');
		}
		foreach ( $admin as $item ) {
			$item['create_time'] = date('Y-m-d H:i:s',$item['create_time']);
			$admins[] = $item;
		}
		$this->assign('admins',$admins);
		$this->assign('role',$role);
		$this->assign('admin',$admin);
	    return $this->fetch();
	}


	/**
	 * 删除管理员方法
	 */
	public function del(){
		$res = Admin::destroy(input('admin_id'));
		if ($res){
			$this->success('已删除');
		}else{
			$this->error('删除失败');
		}
	}


	/**
	 * 添加管理员页面
	 */
	public function add(){
		$role = AuthRole::all();
		$this->assign('role',$role);
		return $this->fetch();
	}


	/**
	 * 添加用户方法
	 */
	public function addAdmin(){
		$post = input('post.');
		$validate = Loader::validate('CheckAddAdmin');
		if(!$validate->check($post)){
			$this->error($validate->getError());
		}
		$post['create_time'] = time();
		$post['password'] = md5($post['password'] );
		$res = Admin::create($post);
		$post['role_id'] = (array_keys($post['role_id']));
		foreach ($post['role_id']  as $item ) {
			$ok = Db::name('auth_role_admin')
				->data(['role_id'=>$item,'admin_id'=>$res->admin_id])
				->insert();
		}
		if ($res && $ok){
			$this->success('添加成功');
		}else{
			$this->error('添加失败');
		}
	}


	/**
	 * 修改页面
	 * @return mixed
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws \think\exception\DbException
	 */
	public function edit(){
		$role = AuthRole::all();
		$admin = Admin::get(['admin_id'=>input('admin_id')]);
		$role_id = Db::name('auth_role_admin')->where('admin_id',input('admin_id'))->column('role_id');
		$this->assign('admin',$admin);
		$this->assign('role',$role);
		$this->assign('role_id',$role_id);
		return $this->fetch();
	}


	public function editAdmin(){
		$post = input('post.');
		$validate = Loader::validate('CheckAddAdmin');
		if(!$validate->check($post)){
			$this->error($validate->getError());
		}
		Db::name('auth_role_admin')->where('admin_id',$post['admin_id'])->delete();
		$post['password'] = md5($post['password']);
		$res = Admin::where('admin_id',$post['admin_id'])->update($post);
		$role_id = array_keys($post['role_id']);
		foreach ( $role_id as $item ) {
			$ok = Db::name('auth_role_admin')
				->data(['role_id'=>$item,'admin_id'=>$post['admin_id']])
				->insert();
		}
		if ($res || $ok){
			$this->success('更改成功');
		}else{
			$this->error('更改失败');
		}
	}
}